package com.cxhc.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import com.cxhc.common.utils.spring.SpringUtils;
import com.google.common.collect.ImmutableMap;
import io.minio.errors.MinioException;
import org.springframework.web.multipart.MultipartFile;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.http.Method;

/**
 * Minio 文件存储工具类
 *
 * @author ruoyi
 */
public class MinioUtil {
    /**
     * 上传文件
     *
     * @param bucketName 桶名称
     * @param fileName
     * @throws IOException
     */
    public static String uploadFile(String bucketName, String fileName, MultipartFile multipartFile) throws IOException {

        String url = "";
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try (InputStream inputStream = multipartFile.getInputStream()) {
            // 尝试上传文件
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .stream(inputStream, multipartFile.getSize(), -1)
                    .contentType(multipartFile.getContentType())
                    .build());

            // 获取预签名URL
            url = minioClient.getPresignedObjectUrl(
                    GetPresignedObjectUrlArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .method(Method.GET)
                            .build());
            // 移除查询参数部分
            url = url.substring(0, url.indexOf('?'));
            return url; // ServletUtils.urlDecode(url)
        } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException e) {
            // 更具体的异常处理
            throw new IOException("Failed to upload file: " + e.getMessage(), e);
        }
    }

    public static Map<String,String> upload(String bucketName, String fileName, MultipartFile multipartFile) throws IOException {
        String url = "";
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try (InputStream inputStream = multipartFile.getInputStream()) {
            // 尝试上传文件
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .stream(inputStream, multipartFile.getSize(), -1)
                    .contentType(multipartFile.getContentType())
                    .build());

            // 获取预签名URL
            url = minioClient.getPresignedObjectUrl(
                    GetPresignedObjectUrlArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .method(Method.GET)
                            .build());
            // 移除查询参数部分
            url = url.substring(0, url.indexOf('?'));
            return ImmutableMap.of("url", url,"name", fileName);
        } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException e) {
            // 更具体的异常处理
            throw new IOException("Failed to upload file: " + e.getMessage(), e);
        }
    }
}
